package com.swensen.game.data;

import java.util.ArrayList;
import java.util.List;

public class AnimatedTile extends Tile {

	private static final long serialVersionUID = 3457929543980368665L;
	private List<BasicTile> tiles;
	private int currentStep = 0;
	
	private enum AnimationSpeed {
		FASTEST,
		FAST,
		MEDIUM,
		SLOW,
		SLOWEST;
	}
	
	private AnimationSpeed speed = AnimationSpeed.FASTEST;
	
	public AnimatedTile setSpeed(AnimationSpeed speed) {
		this.speed = speed;
		return this;
	}
	
	public AnimatedTile(TileId id, List<BasicTile> tiles) {
		super(id, TileType.ANIMATED);
		this.tiles = tiles;
	}

	@Override
	public TileId getImageData() {
		int speedInt = speed.ordinal() * 3 + 2;
		if (tiles.size() <= 0 || currentStep / speedInt >= tiles.size()) {
			return null;
		}
		
		currentStep = ++currentStep % (tiles.size() * speedInt);
		return tiles.get(currentStep / speedInt).getImageData();
	}
	
	@Override
	public TileId getStillImageData() {
		if (tiles.size() <= 0) {
			return null;
		}
		return tiles.get(0).getImageData();
	}

	@Override
	public AnimatedTile copy() {
//		List<BasicTile> deepCopy = new ArrayList<BasicTile>();
//		for (BasicTile tile : tiles) {
//			deepCopy.add(tile.copy());
//		}
		return new AnimatedTile(id, tiles).setSpeed(speed);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null || id.getFileName() == null) ? 0 : id.getFileName().hashCode());
		return result;
	}
	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Tile other = (Tile) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (id.getFileName() == null) {
			if (other.id.getFileName() != null)
				return false;
		} else if (!id.getFileName().equals(other.id.getFileName()))
			return false;
		return true;
	}
}
